package com.yeehot.iot_manager.utils;



import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class OkHttpUtils {
    private Handler mUiHandler = new Handler(Looper.getMainLooper());
    private OkHttpClient mOkHttpClient;
    private  String baseurl="http://192.168.10.107:9998/api/manager/";
    private OkHttpUtils(){


        mOkHttpClient = new OkHttpClient.Builder()

                .build();

    }
    public static OkHttpUtils sInstance = new OkHttpUtils();
    public static OkHttpUtils getInstance(){
        return sInstance;
    }


    //GET：请求地址http://www.imooc.com/api/okhttp/getmethod?username=hyman&password=123

    public void doGet(String url,INetCallBack callBack) {

        Request request = new Request.Builder()
                .url(url)
                .build();
        executeRequest(callBack, request);

    }

    public void doPost(String url, HashMap<String,String> params,INetCallBack callBack){

        FormBody requestBody = null;
        FormBody.Builder formBodyBuilder   = new FormBody.Builder();

        if (params!=null){
            for (String param:params.keySet()){
                formBodyBuilder.add(param,params.get(param));
            }
        }


        Request request = new Request.Builder()
                .url(url)
                .post(formBodyBuilder.build())
                .build();
        executeRequest(callBack, request);
    }
    public void doPostMultiPart(String url, HashMap<String,String> params,INetCallBack callBack){

        MultipartBody.Builder multipartBodyBuilder   = new MultipartBody.Builder();
        multipartBodyBuilder.setType(MultipartBody.FORM);

        if (params!=null){
            for (String param:params.keySet()){
                multipartBodyBuilder.addFormDataPart(param,params.get(param));
            }
        }

        Request request = new Request.Builder()
                .url(url)
                .post(multipartBodyBuilder.build())
                .build();
        executeRequest(callBack, request);
    }
    public void doPostJson(String url,String jsonStr, INetCallBack callBack){
        url=baseurl+url;
        MediaType jsonMediaType = MediaType.parse("application/json");
        RequestBody requestBody =RequestBody.create(jsonMediaType,jsonStr);

        //System.out.println(url+"--"+jsonStr);
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        executeRequest(callBack, request);
    }

    private void executeRequest(INetCallBack callBack, Request request) {
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                mUiHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.onFailed(e);
                    }
                });
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String resStr = null;
                try {
                    resStr = response.body().string();
                } catch (IOException e) {
                    mUiHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            callBack.onFailed(e);
                        }
                    });
                    return;
                }
                String finalRespStr = resStr;
                mUiHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.onSuccess(finalRespStr);
                    }
                });
            }
        });
    }
}

